package 划分链表;


/**
 * @author 冷加宝
 * @version 1.0
 */
public class PartitionList {
    public static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }

        public ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode partitionList(ListNode head, int x){
        ListNode LeftHead = null, LeftTail = null; // <x区域
        ListNode RightHead = null, RightTail = null;// >=x区域
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = null;
            if(head.val < x){
                if(LeftHead == null){
                    LeftHead = head;
                }else{
                    LeftTail.next = head;
                }
                LeftTail = head;
            }else{
                if(RightHead == null){
                    RightHead = head;
                }else{
                    RightTail.next = head;
                }
                RightTail = head;
            }
            head = next;
        }

        if(LeftHead == null){
            return RightHead;
        }
        LeftTail.next = RightHead;
        return LeftHead;
    }
}
